package tt.dz.biz.dao.user;

import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.RoleVO;

@Repository("roleDAO")
public class RoleDAO
  extends BaseDAO
{
  private static final String QUERY_ROLE_BY_SYS = "select  a.role_id,a.sys_code,a.role_name,a.remark,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_role as a  where a.sys_code = :sysCode and a.is_valid = 1 ";
  private static final String LIST_ALL_VALID_ROLE = "select  a.role_id,a.sys_code,a.role_name,a.remark,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_role as a where a.is_valid = 1";
  private static final String LIST_ALL_ROLE = "select  a.role_id,a.sys_code,a.role_name,a.remark,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid   from pub_role as a ";
  private static final String INSERT_ROLE = "insert into pub_role(role_id,sys_code,role_name,remark,created_by,created_time,updated_by,updated_time,is_valid)  values(:roleId,:sysCode,:roleName,:remark,:createdBy,:createdTime,:updatedBy,:updatedTime,:isValid )";
  private static final String UPDATE_ROLE = "update  pub_role set sys_code = :sysCode,role_name = :roleName,remark = :remark,created_by = :createdBy,created_time = :createdTime,updated_by = :updatedBy,updated_time = :updatedTime,is_valid = :isValid where role_id = :roleId ";
  private static final String GET_USER_ROLE = "select t3.role_name,t3.role_id,t2.sys_code, t1.name, t1.manager_id from pub_oper_list t1 left join pub_oper_role t2 on t1.manager_id = t2.manager_id and t2.is_valid = 1 left join pub_role t3 on t2.sys_code = t3.sys_code and t2.role_id = t3.role_id and t3.is_valid = 1 where t1.is_valid = 1  and (t1.mobile_no =:mobileNo or t1.nick_name=:nickName or t1.email=:email)";
  
  public RoleVO queryRoleByID(String roleID, String sysCode)
  {
    String sql = "select  a.role_id,a.sys_code,a.role_name,a.remark,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid   from pub_role as a  where a.role_id=:roleId and a.sys_code=:sysCode";
    RoleVO paramrole = new RoleVO();
    paramrole.setRoleId(roleID);
    paramrole.setSysCode(sysCode);
    try
    {
      return (RoleVO)queryForObject(sql, paramrole, RoleVO.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      return null;
    }
    catch (IncorrectResultSizeDataAccessException e)
    {
      return null;
    }
    catch (DataAccessException e)
    {
      LogUtil.error(e.getMessage(), e);
    }
    return null;
  }
  
  public List<RoleVO> queryRoleBySys(RoleVO roleVO)
  {
    return queryForList("select  a.role_id,a.sys_code,a.role_name,a.remark,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_role as a  where a.sys_code = :sysCode and a.is_valid = 1 ", roleVO, RoleVO.class);
  }
  
  public List<RoleVO> listAllRole()
  {
    return queryForList("select  a.role_id,a.sys_code,a.role_name,a.remark,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid   from pub_role as a ", null, RoleVO.class);
  }
  
  public List<RoleVO> listAllValidRole()
  {
    return queryForList("select  a.role_id,a.sys_code,a.role_name,a.remark,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_role as a where a.is_valid = 1", null, RoleVO.class);
  }
  
  public int insertRole(RoleVO roleVO)
  {
    return save("insert into pub_role(role_id,sys_code,role_name,remark,created_by,created_time,updated_by,updated_time,is_valid)  values(:roleId,:sysCode,:roleName,:remark,:createdBy,:createdTime,:updatedBy,:updatedTime,:isValid )", roleVO);
  }
  
  public int updateRole(RoleVO roleVO)
  {
    return save("update  pub_role set sys_code = :sysCode,role_name = :roleName,remark = :remark,created_by = :createdBy,created_time = :createdTime,updated_by = :updatedBy,updated_time = :updatedTime,is_valid = :isValid where role_id = :roleId ", roleVO);
  }
}
